require 'yaml'
IRCBot::Plugin.define "whatis" do
	help_string	"Information DB. Retrieve with \"Marco, what is/what's/what are <stuff>?\" and add/update just by saying that something is something or other."
	
	@facts = YAML::load(File.open($config['global']['plugin_data_dir']+'/facts.yml'))
	if !@facts then @facts = {}; end
	
	def command(user,target,message,hostmask)
		command = message.split[0]
		message = message.sub(command,"").strip
		
		case command
			when 'set'
				if message.split('=').count >= 2 then
					fact = message.split('=')[0].downcase.strip
					factoid = message.sub(message.split('=')[0]+'=',"").strip
					if @facts[fact].class != Array then @facts[fact] = []; end
					@facts[fact].push factoid
					privmsg(target,"Fact added.")
					File.open($config['global']['plugin_data_dir']+'/facts.yml','w') do |file|
						file << @facts.to_yaml
					end
				else
					privmsg(target,"RTFM.")
				end
			when 'showall'
				if @facts[message].class == Array then
					privmsg(target,"#{message} can be: #{@facts[message].join(', ')}.")
				elsif @facts[message] then
					privmsg(target,"#{message} can be: #{@facts[message]}.")
				else
					privmsg(target,"I don't know what that is.")
				end
		end
	end
	
	def privmsg_function(user,target,message,hostmask)
		if message.start_with?("#{$config[$network]['nickname']}, what is ") or message.start_with?("#{$config[$network]['nickname']}, what's ") or message.start_with?("#{$config[$network]['nickname']}, what are ") then
			if message.start_with?("#{$config[$network]['nickname']}, what is ") then
				message = message.sub("#{$config[$network]['nickname']}, what is ","")
				connector = ' is '
			elsif message.start_with?("#{$config[$network]['nickname']}, what are ") then
				message = message.sub("#{$config[$network]['nickname']}, what are ","")
				connector = ' are '
			else
				message = message.sub("#{$config[$network]['nickname']}, what's ","")
				connector = '\'s '
			end
			if message[-1..-1] == '?' then message[-1] = ''; end
			message = message.downcase.strip
			
			if @facts[message] != nil and @facts[message].class == Array then
				privmsg(target,"#{message.capitalize}#{connector}#{@facts[message][rand(@facts[message].count)]}")
			elsif @facts[message] then
				privmsg(target,"#{message.capitalize}#{connector}#{@facts[message]}")
			else
				privmsg(target,"I don't know.")
			end
		elsif message =~ /^(.+?)\s(is|are)\s(.+?)/ then
			if message =~ /^(.+?)\sare\s(.+?)/ then
				thing = message.split(' are ')[0].sub('"','').strip
				definition = message.sub(thing+' are ','').strip
				are = true
			else
				thing = message.split(' is ')[0].sub('"','').strip
				definition = message.sub(thing+' is ','').strip
				are = false
			end
			thing = thing.downcase
			
			if @facts[thing].class != Array then @facts[thing] = [];end
			@facts[thing].push definition
			File.open($config['global']['plugin_data_dir']+'/facts.yml','w') do |file|
				file << @facts.to_yaml
			end
			#privmsg(target,"I've just learned that #{thing.capitalize} #{(are ? 'are' : 'is')} #{definition}.")
		end
	end
end
